/*
 * Copyright (C) 2008  Young,Fey <fey.young@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */ 
package com.youngfey.wordmaster.database.tables

import com.youngfey.database.annotation.*
import com.youngfey.wordmaster.database.*
import static com.youngfey.wordmaster.database.Database.*

@HasMany(['ArticleText', 'SentenceIndex', 'CategoryIndex', 'PropertyIndex'])
public class Article {

    String title

    // <editor-fold defaultstate="collapsed" desc=" Generated Code (by gRDB)">
    Integer id
    

    boolean equals(Object o) {
        id != null && o instanceof Article && id == o.getId()
    }

    static createTable() {
        execute("CREATE TABLE Article (Id INT NOT NULL GENERATED ALWAYS AS IDENTITY CONSTRAINT ARTICLE_PK PRIMARY KEY, Title VARCHAR(255) NOT NULL)")
        
    }

    boolean isValid() {
        title?.size() <= 255
    }



    static Article create(title) {
        def instance = new Article(title: title)
        instance.insert()
        instance
    }

    def insert() {
        if(id == null && isValid()) {
            id = executeInsert("INSERT INTO Article (Title) VALUES (${title})")[0][0]
            if(id != null) {
                
                return
            }
        }

        throw new IllegalStateException()
    }

    //capitalize;
    static Article find(Integer id) {
        if(id == null) return null

        

        def row = firstRow("SELECT * FROM Article WHERE Id = $id")
        
        row ? new Article(id: row.Id, title: row.title) : null
        
    }

    static ArrayList<Article> findBySql(stmt) {
        def rows = []
        eachRow(stmt) {row ->
            rows << new Article(id: row.Id, title: row.title)
        }
        
        rows
    }

    static ArrayList<Article> findAll() {
        findBySql('SELECT * FROM Article')
    }

    
    static ArrayList<Article> findAll(String keyword) {
        findBySql("SELECT * FROM Article WHERE (Title LIKE $keyword)")
    }
    

    private executeUpdate(stmt) {
        def updated = executeUpdate(stmt)
        if(updated != 1) throw new IllegalStateException()
        
    }

    def update() {
        if(id != null && isValid()) {
            executeUpdate("UPDATE Article SET Title = ${title} WHERE Id = $id")
        }
    }

    static delete(Integer id) {
        execute("DELETE FROM Article WHERE Id = $id")
        
    }

    def delete() {
        if(id != null) delete(id)
    }


    static ArrayList<Article> findByTitle(value) {
        findBySql("SELECT * FROM Article WHERE Title = ${value}")
    }



    static ArrayList<Article> findByTitleLike(String keyword) {
        findBySql("SELECT * FROM Article WHERE Title LIKE $keyword")
    }



    def updateTitle(value) {
        this.title = value

        if(id != null && isValid()) {
            executeUpdate("UPDATE Article SET Title = ${value} WHERE Id = $id")
        } else {
            throw new IllegalStateException("Update Error: value = $value")
        }
    }



    ArrayList<ArticleText> getArticleTextList() {
        ArticleText.findByArticle(this)
    }

    ArrayList<SentenceIndex> getSentenceIndexList() {
        SentenceIndex.findByArticle(this)
    }

    ArrayList<CategoryIndex> getCategoryIndexList() {
        CategoryIndex.findByArticle(this)
    }

    ArrayList<PropertyIndex> getPropertyIndexList() {
        PropertyIndex.findByArticle(this)
    }

    // </editor-fold>
}
